package com.wh.stock.controller;

import com.wh.basic.vo.AjaxResult;
import com.wh.stock.service.IStockService;
import com.wh.stock.pojo.Stock;
import com.wh.stock.dto.StockQueryDto;
import com.wh.system.annotation.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/stock")
public class StockController {

    @Autowired
    public IStockService stockService;

    /**
     * 接口：添加或修改
     * @param stock 传递的实体
     * @return AjaxResult 响应给前端
     */
    @PreAuthorize(sn = "stock/add",name = "添加产品库")
    @PutMapping("/add")
    public AjaxResult add(@RequestBody Stock stock) {
        stockService.save(stock); //需要在逻辑层进行minio的图片上传。。还用这个方式吗
        return new AjaxResult();
    }

    @PreAuthorize(sn = "stock/update",name = "修改产品库存")
    @PutMapping("/upd")
    public AjaxResult update(@RequestBody Stock stock) {

        stockService.updById(stock);
        return new AjaxResult();
    }
    @PreAuthorize(sn = "stock/addOrUpdate",name = "添加或修改产品库存")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Stock stock) {
        if ( stock.getId() != null){
            stockService.updateById(stock);
        }else{
            stockService.save(stock); //需要在逻辑层进行minio的图片上传。。还用这个方式吗
        }
        return new AjaxResult();

    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */

    @PreAuthorize(sn = "stock/del",name = "根据id删除产品库存")
    @DeleteMapping(value = "/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        stockService.removeById(id);
        return new AjaxResult();
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */

    @PreAuthorize(sn = "stock/patchDel",name = "批量删除产品库存")
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids) {
        stockService.removeByIds(ids);
        return new AjaxResult();
    }

    /**
    * 接口：查询单个对象
    * @param id
    */

    @PreAuthorize(sn = "stock/findOne",name = "根据id查询产品库存")
    @GetMapping("/{id}")
    public Stock findOne(@PathVariable("id") Long id) {
        return stockService.getById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */

    @GetMapping
    public List<Stock> findAll() {
        return stockService.list();
    }


    /**
     * 接口：分页查询或高级查询
     * @param stockQueryDto 查询对象
     * @return IPage<Stock> 分页对象
     */

    @PreAuthorize(sn = "stock/findByPage",name = "分页查询产品库存")
    @PostMapping
    public IPage<Stock> findByPage(@RequestBody StockQueryDto stockQueryDto) {
        return stockService.findByPage(stockQueryDto);
    }
}
